package com.taotao.manage.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;
import com.taotao.common.pojo.EasyUIResult;
import com.taotao.manage.pojo.Content;
import com.taotao.manage.service.ContentService;

@Controller
@RequestMapping("content")
public class ContentController {
	
	@Autowired
	private ContentService contentService;
	
	//分页查询,根据内容目录
	/*@RequestMapping(method = RequestMethod.GET)
	public ResponseEntity<EasyUIResult> queryContent(@RequestParam("categoryId")Long categoryId
			,@RequestParam("page")Integer pageNum
			,@RequestParam("rows")Integer pageSize){
	try {
				Content record = new Content();
		record.setCategoryId(categoryId);
		PageInfo<Content> pageInfo = contentService.queryPageListByWhere(record, pageNum, pageSize);
		EasyUIResult easyUIResult = new EasyUIResult(pageInfo.getTotal(),pageInfo.getList());
		return ResponseEntity.ok(easyUIResult);
		} catch (Exception e) {
			e.printStackTrace();
		}
	return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}*/
	
	//分页查询并按时间倒序
	@RequestMapping(method = RequestMethod.GET)
	public ResponseEntity<EasyUIResult> queryContent(@RequestParam("categoryId")Long categoryId
			,@RequestParam("page")Integer pageNum
			,@RequestParam("rows")Integer pageSize){
	try {
			
		PageInfo<Content> pageInfo = contentService.queryPageListByWhereOrderBy(categoryId, pageNum, pageSize);
		EasyUIResult easyUIResult = new EasyUIResult(pageInfo.getTotal(),pageInfo.getList());
		return ResponseEntity.ok(easyUIResult);
		} catch (Exception e) {
			e.printStackTrace();
		}
	return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public ResponseEntity<Void> saveContent(Content content){
		try {
			Integer count = contentService.saveSelective(content);
			if (count == 1) {
				return ResponseEntity.status(HttpStatus.CREATED).build();
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		
	}
}
